﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DomainDriver.Presentation.Silverlight.Base
{
    public static class DomainViewExtensions
    {
        public const string DefaultRefreshMethodName = "ReloadView";
        public const string ClearViewMethodName = "ClearView";
        public const string ReloadViewMethodName = "ReloadView";
        public const string LockViewMethodName = "LockView";
        public const string UnlockViewMethodName = "UnlockView";

        public static bool ValidateAndCallMethodByName(this IDomainView domainView, string methodName)
        {
            if (!ValidateAndClear(domainView))
            { return false; }

            if (string.IsNullOrWhiteSpace(methodName))
            { methodName = DefaultRefreshMethodName; }

            if (methodName == DefaultRefreshMethodName)
            { domainView.ReloadView(); }
            else if (methodName == ClearViewMethodName)
            { domainView.ClearView(); }
            else if (methodName == ReloadViewMethodName)
            { domainView.ReloadView(); }
            else if (methodName == LockViewMethodName)
            { domainView.LockView(); }
            else if (methodName == UnlockViewMethodName)
            { domainView.UnlockView(); }
            else
            { throw new InvalidOperationException("Invalid method name specified."); }
            return true;
        }

        public static bool ValidateAndRefresh(this IDomainView domainView)
        {
            if (!ValidateAndClear(domainView))
            { return false; }

            domainView.ReloadView();
            return true;
        }

        public static bool ValidateAndLock(this IDomainView domainView)
        {
            if (!ValidateAndClear(domainView))
            { return false; }

            domainView.LockView();
            return true;
        }

        public static bool ValidateAndUnlock(this IDomainView domainView)
        {
            if (!ValidateAndClear(domainView))
            { return false; }

            domainView.UnlockView();
            return true;
        }

        public static bool ValidateAndClear(this IDomainView domainView)
        {
            if (DesignerProperties.IsInDesignTool)
            { return false; }

            if (domainView.Controller == null)
            {
                domainView.ClearView();
                return false;
            }

            if (!domainView.Controller.IsReadyToDisplay)
            {
                domainView.ClearView();
                return false;
            }

            return true;
        }
    }
}